const Router = require('koa-router')
const address = new Router()
const db = require('../untils/database')

address.post('/shippingAdderssList',async(ctx)=>{
    let userId = ctx.request.body.userId
    let sql = `SELECT * FROM shipping_address where userId = '${userId}'`;
    let result = await new Promise((resolve,reject)=>{
        return db.query(sql,(err,data)=>{
            if(err) throw err;
            if(data.length>0){
                resolve(data)
            }else{
                resolve(false)
            }
        })   
    })
    if(result){
        ctx.body = {
            message: result,
            msg: 'success'
        }
    }else{
        ctx.body = {
            message: null,
            msg: 'successs'
        }
    }
})

address.put('/modifyShippingAdderssList',async(ctx)=>{
    let id = ctx.request.body.id
    let userId = ctx.request.body.userId
    let name = ctx.request.body.name
    let tel = ctx.request.body.tel
    let province = ctx.request.body.province
    let city = ctx.request.body.city
    let county = ctx.request.body.county
    let addressDetail = ctx.request.body.addressDetail
    let areaCode = ctx.request.body.areaCode
    let isDefault = ctx.request.body.isDefault  
    let address = ctx.request.body.address 
    console.log(isDefault);
    if(isDefault === true){
        let sql = `UPDATE shipping_address set isDefault = 0 where userId = '${userId}'`;
        await new Promise((resolve,reject)=>{
            return db.query(sql,(err,data)=>{
                if(err) throw err;
                if(data.length>0){
                    resolve(data)
                }else{
                    resolve(false)
                }
            })   
        })
    }
    let sql1 =`UPDATE shipping_address SET  name='${name}', tel='${tel}', province='${province}', city='${city}', county='${county}', addressDetail='${addressDetail}', areaCode='${areaCode}', isDefault=${isDefault},address='${address}' WHERE id='${id}' `
    let result1 = await new Promise((resolve,reject)=>{
        return db.query(sql1,(err,data)=>{
            if(err) throw err;
            if(data.length>0){
                resolve(data)
            }else{
                resolve(false)
            }
        })   
    })
    if(result1){
        ctx.body = {
            message: result1,
            msg: 'success'
        }
    }else{
        ctx.body = {
            message: null,
            msg: '系统开小差'
        }
    }
    
   
})

address.post('/addShippingAdderssList',async(ctx)=>{
    let userId = ctx.request.body.userId
    let name = ctx.request.body.name
    let tel = ctx.request.body.tel
    let province = ctx.request.body.province
    let city = ctx.request.body.city
    let county = ctx.request.body.county
    let addressDetail = ctx.request.body.addressDetail
    let areaCode = ctx.request.body.areaCode
    let isDefault = ctx.request.body.isDefault  
    let address = ctx.request.body.address
    if(isDefault === true){
        let sql = `UPDATE shipping_address set isDefault = 0 where userId = '${userId}'`;
        await new Promise((resolve,reject)=>{
            return db.query(sql,(err,data)=>{
                if(err) throw err;
                if(data.length>0){
                    resolve(data)
                }else{
                    resolve(false)
                }
            })   
        })
    }
    let sql1 =`INSERT INTO shipping_address (userId, name, tel, province, city, county, addressDetail, areaCode, isDefault,address ) values ('${userId}', '${name}', '${tel}', '${province}', '${city}', '${county}', '${addressDetail}', '${areaCode}', ${isDefault}, '${address}')`
    let result = await new Promise((resolve,reject)=>{
        return db.query(sql1,(err,data)=>{
            if(err) throw err;
            if(data.length>0){
                resolve(data)
            }else{
                resolve(false)
            }
        })   
    })
    if(result){
        ctx.body = {
            message: result,
            msg: 'success'
        }
    }else{
        ctx.body = {
            message: null,
            msg: '系统开小差'
        }
    }
})

address.delete('/deleteShippingAdderssList',async(ctx)=>{
    let id = ctx.request.body.id 
    let sql = `DELETE FROM shipping_address WHERE id =${id}`;
    let result = await new Promise((resolve,reject)=>{
        return db.query(sql,(err,data)=>{
            if(err) throw err;
            let obj = {
                data,
                msg: 'success',
            }
            resolve(obj)
        })   
    })
    if(result){
        ctx.body = result
    }
})

module.exports = address;